package com.situ.day19;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.jar.Attributes.Name;

import javax.naming.spi.DirStateFactory.Result;

import org.junit.Test;

import com.situ.day18.JDBCUtil;

public class Demo {
	@Test
	public void test1() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		String sql1 = "UPDATE account SET money = money - 1000 WHERE name = '张三';";
		String sql2 = "UPDATE account SET money = money + 1000 WHERE name = '李四';";
		try {
			connection = JDBCUtil.getConnection();
			connection.setAutoCommit(false);
			preparedStatement = connection.prepareStatement(sql1);
			preparedStatement.executeUpdate();
			// 算术异常
			int i = 3 / 0;
			preparedStatement = connection.prepareStatement(sql2);
			preparedStatement.executeUpdate();
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}
	@Test
	public void test2() {
		Connection connection = null;
		CallableStatement callableStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			callableStatement = connection.prepareCall("CALL show_teachers");
			resultSet = callableStatement.executeQuery();
			while (resultSet.next()) {
				int dId = resultSet.getInt("id");
				String dName = resultSet.getString("name");
				int dAge = resultSet.getInt("age");
				System.out.println(dId);
				System.out.println(dName);
				System.out.println(dAge);
				System.out.println("--------------------");				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, callableStatement, resultSet);
		}
	}
	@Test
	public void test3() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int id = 5;
		try {
			connection = JDBCUtil.getConnection();
			connection.setAutoCommit(false);
			String sql = "SELECT id,name,age,gender FROM student WHERE id = ?;";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, 5);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int stId = resultSet.getInt("id");
				String stName = resultSet.getString("name");
				int stAge = resultSet.getInt("age");
				String stGender = resultSet.getString("gender");
				System.out.println(stId);
				System.out.println(stName);
				System.out.println(stAge);
				System.out.println(stGender);
			}
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
	@Test
	public void test4() {
		PreparedStatement preparedStatement = null;
		Connection connection = null;
		String name = "可爱";
		int age = 26;
		String gender = "男";
		try {
			connection = JDBCUtil.getConnection();
			connection.setAutoCommit(false);
			String sql = "INSERT INTO student(`name`,age,gender) VALUES (?,?,?);";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, name);
			preparedStatement.setInt(2, 26);
			preparedStatement.setString(3, gender);
			preparedStatement.executeUpdate();
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
	//删除
	@Test
	public void test5() {
		PreparedStatement preparedStatement = null;
		Connection connection = null;
		int id = 6;
		try {
			connection = JDBCUtil.getConnection();
			connection.setAutoCommit(false);
			String sql = "DELETE FROM student WHERE id = ?;";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, 6);
			int count = preparedStatement.executeUpdate();
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
	@Test
	public void test6() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int id = 5;
		try {
			connection = JDBCUtil.getConnection();
			connection.setAutoCommit(false);
			String sql = "UPDATE student SET `name` = '小可爱' WHERE id = ?;";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			int count = preparedStatement.executeUpdate();
			System.out.println(count);
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
	

}
